home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Graph / Kruskal.pm
Encoding:
Perl POD Document  |  1999-12-28  |  11.7 KB  |  444 lines

  1.  
  2.  
  3. package Graph::Kruskal;
  4.  
  5. use strict;
  6. use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION
  7.             $number_of_edges $number_of_vortices @V @E @T);
  8.  
  9. require Exporter;
  10.  
  11. @ISA = qw(Exporter);
  12.  
  13. @EXPORT = qw();
  14.  
  15. @EXPORT_OK = qw(define_vortices define_edges
  16.                 heapify makeheap heapsort
  17.                 find union kruskal example);
  18.  
  19. %EXPORT_TAGS = (all => [@EXPORT_OK]);
  20.  
  21. $VERSION = '2.0';
  22.  
  23. use Carp;
  24.  
  25. $number_of_vortices = 0;
  26. $number_of_edges = 0;
  27.  
  28. sub example
  29. {
  30.     my($costs) = 0;
  31.     my($k);
  32.  
  33.     print "\n";
  34.     print "+++ Kruskal's Algorithm for Minimal Spanning Trees in Graphs +++";
  35.     print "\n";
  36.  
  37.     &define_vortices(2,3,5,7,11,13,17,19,23,29,31);
  38.  
  39.     print "\nVortices:\n\n";
  40.  
  41.     for ( $k = 1; $k <= $#V; ++$k )
  42.     {
  43.         if (defined $V[$k]) { print "$k\n"; }
  44.     }
  45.  
  46.     &define_edges( 2,13,3, 3,13,2, 5,13,1, 3,5,2, 3,29,21, 23,29,3,
  47.      23,31,2, 5,31,15, 5,7,10, 2,11,2, 7,11,2, 7,19,5, 11,19,2,
  48.      7,31,4, 3,17,3, 17,23,3, 7,17,3 );
  49.  
  50.     print "\nEdges:\n\n";
  51.  
  52.     for ( $k = 1; $k <= $#E; ++$k )
  53.     {
  54.         print ${$E[$k]}{'from'}, " <-> ", ${$E[$k]}{'to'}, " = ",
  55.             ${$E[$k]}{'cost'}, "\n";
  56.     }
  57.  
  58.     &kruskal();
  59.  
  60.     print "\nEdges in minimal spanning tree:\n\n";
  61.  
  62.     for ( $k = 1; $k <= $#T; ++$k )
  63.     {
  64.         print ${$T[$k]}{'from'}, " <-> ", ${$T[$k]}{'to'}, " = ",
  65.             ${$T[$k]}{'cost'}, "\n";
  66.         $costs += ${$T[$k]}{'cost'};
  67.     }
  68.  
  69.     print "\nTotal costs: $costs\n\n";
  70. }
  71.  
  72. sub define_vortices
  73. {
  74.     undef @V;
  75.     $number_of_vortices = 0;
  76.     foreach (@_)
  77.     {
  78.         ($_ > 0) || croak "Graph::Kruskal::define_vortices(): vortex number not positive\n";
  79.         $V[$_] = -1;
  80.         ++$number_of_vortices;
  81.     }
  82. }
  83.  
  84. sub define_edges
  85. {
  86.     my($from,$to,$cost);
  87.  
  88.     undef @E;
  89.     $number_of_edges = 0;
  90.     while (@_)
  91.     {
  92.         $from = shift || croak "Graph::Kruskal::define_edges(): missing 'from' vortex number\n";
  93.         $to   = shift || croak "Graph::Kruskal::define_edges(): missing 'to' vortex number\n";
  94.         $cost = shift || croak "Graph::Kruskal::define_edges(): missing edge 'cost' value\n";
  95.         defined $V[$from] || croak "Graph::Kruskal::define_edges(): vortex '$from' not previously defined\n";
  96.         defined $V[$to]   || croak "Graph::Kruskal::define_edges(): vortex '$to' not previously defined\n";
  97.         ($from != $to)    || croak "Graph::Kruskal::define_edges(): vortices 'from' and 'to' are the same\n";
  98.         $E[++$number_of_edges] =
  99.             { 'from' => $from, 'to' => $to, 'cost' => $cost };
  100.     }
  101. }
  102.  
  103. sub heapify             # complexity: O(ld n)
  104. {
  105.     my($i,$n) = @_;
  106.     my($i2,$i21,$j,$swap);
  107.  
  108.     while ($i < $n)
  109.     {
  110.         $j   = $i;
  111.         $i2  = $i * 2;
  112.         $i21 = $i2 + 1;
  113.         if ($i2 <= $n)
  114.         {
  115.             if (${$E[$i]}{'cost'} > ${$E[$i2]}{'cost'})
  116.             {
  117.                 $j = $i2;
  118.                 if ($i21 <= $n)
  119.                 {
  120.                     if (${$E[$i2]}{'cost'} > ${$E[$i21]}{'cost'}) { $j = $i21; }
  121.                 }
  122.             }
  123.             else
  124.             {
  125.                 if ($i21 <= $n)
  126.                 {
  127.                     if (${$E[$i]}{'cost'} > ${$E[$i21]}{'cost'}) { $j = $i21; }
  128.                 }
  129.             }
  130.         }
  131.         if ($i != $j)
  132.         {
  133.             $swap  = $E[$i];
  134.             $E[$i] = $E[$j];
  135.             $E[$j] = $swap;
  136.             $i = $j;
  137.         }
  138.         else { $i = $n; }
  139.     }
  140. }
  141.  
  142. sub makeheap            # complexity: O(n ld n)
  143. {
  144.     my($n) = @_;
  145.     my($k);
  146.  
  147.     for ( $k = $n - 1; $k > 0; --$k ) { &heapify($k, $n); }
  148. }
  149.  
  150.  
  151. sub heapsort            # complexity: O(n ld n)
  152. {
  153.     my($n) = @_;
  154.     my($k,$swap);
  155.  
  156.     for ( $k = $n - 1; $k > 0; --$k ) { &heapify($k, $n); }
  157.  
  158.     for ( $k = $n; $k > 1; --$k )
  159.     {
  160.         $swap  = $E[1];
  161.         $E[1]  = $E[$k];
  162.         $E[$k] = $swap;
  163.         &heapify(1, $k - 1);
  164.     }
  165. }
  166.  
  167. sub find
  168. {
  169.     my($i) = @_;
  170.     my($j,$k,$t);
  171.  
  172.     $j = $i;
  173.     while ($V[$j] > 0) { $j = $V[$j]; } # find root element (= set identifier)
  174.     $k = $i;
  175.     while ($k != $j)                    # height compression of the tree
  176.     {
  177.         $t = $V[$k];
  178.         $V[$k] = $j;
  179.         $k = $t;
  180.     }
  181.     return($j);
  182. }
  183.  
  184. sub union
  185. {
  186.     my($i,$j) = @_;
  187.     my($x);
  188.  
  189.     $x = $V[$i] + $V[$j];    # calculate number of elements in resulting set
  190.     if ($V[$i] > $V[$j])     # which of the two sets contains more elements?
  191.     {
  192.         $V[$i] = $j;         # merge them
  193.         $V[$j] = $x;         # update number of elements
  194.     }
  195.     else
  196.     {
  197.         $V[$j] = $i;         # merge them
  198.         $V[$i] = $x;         # update number of elements
  199.     }
  200. }
  201.  
  202. sub kruskal             # complexity: O(n ld n)   ( where n := |{ Edges }| )
  203. {
  204.     my($n) = $number_of_edges;
  205.     my($v) = $number_of_vortices;
  206.     my($i,$j,$swap);
  207.     my($t) = 0;
  208.  
  209.     undef @T;
  210.     &makeheap($number_of_edges);        # complexity: O(n ld n)
  211.     while (($v > 1) && ($n > 0))
  212.     {
  213.         $swap  = $E[1];
  214.         $E[1]  = $E[$n];
  215.         $E[$n] = $swap;
  216.         &heapify(1, $n - 1);            # complexity: n O(ld n) = O(n ld n)
  217.         $i = find(${$E[$n]}{'from'});   # complexity: n ( 2 find + 1 union ) =
  218.         $j = find(${$E[$n]}{'to'});     #             O( G(n) n ) <= O(n ld n)
  219.         if ($i != $j)
  220.         {
  221.             union($i,$j);
  222.             $T[++$t] = $E[$n];
  223.             --$v;
  224.         }
  225.         --$n;
  226.     }
  227.     return(@T);
  228. }
  229.  
  230. 1;
  231.  
  232. __END__
  233.  
  234. =head1 NAME
  235.  
  236. Graph::Kruskal - Kruskal's Algorithm for Minimal Spanning Trees in Graphs
  237.  
  238. Computes the Minimal Spanning Tree of a given graph according to
  239. some cost function defined on the edges of the graph.
  240.  
  241. =head1 SYNOPSIS
  242.  
  243. =over 4
  244.  
  245. =item *
  246.  
  247. C<use Graph::Kruskal qw(define_vortices define_edges>
  248. C<heapify makeheap heapsort find union kruskal example);>
  249.  
  250. =item *
  251.  
  252. C<use Graph::Kruskal qw(:all);>
  253.  
  254. =item *
  255.  
  256. C<&define_vortices(2,3,5,7,11,13,17,19,23,29,31);>
  257.  
  258. Define a list of vortices (integers > 0)
  259.  
  260. =item *
  261.  
  262. C<&define_edges( 2,13,3, 3,13,2, 5,13,1, 3,5,2, 3,29,21 );>
  263.  
  264. Define (non-directed) edges on the vortices previously defined (always
  265. in triplets: "from" vortice, "to" vortice and cost of that edge)
  266.  
  267. =item *
  268.  
  269. C<&heapify($i,$n);>
  270.  
  271. Main subroutine for sorting the edges according to their costs
  272.  
  273. =item *
  274.  
  275. C<&makeheap($n);>
  276.  
  277. Routine to initialize sorting of the edges
  278.  
  279. =item *
  280.  
  281. C<&heapsort($n);>
  282.  
  283. The famous heapsort algorithm (not needed for Kruskal's algorithm as a whole
  284. but included here for the sake of completeness) for sorting the edges
  285. according to their costs
  286.  
  287. =item *
  288.  
  289. C<&find($i);>
  290.  
  291. =item *
  292.  
  293. C<&union($i,$j);>
  294.  
  295. Disjoint (!) sets are stored as trees in an array in this algorithm. Each
  296. element of some set (a cell in the array) contains a pointer to (the number
  297. of) another element, up to the root element that does not point anywhere,
  298. but contains the (negative) number of elements the set contains. The number
  299. of the root element is also used as an identifier for the set.
  300.  
  301. Example:
  302.  
  303.             i  : |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |
  304.     -------------+-----+-----+-----+-----+-----+-----+-----+-----+
  305.      parent[i] : | -4  | -3  |  1  |  2  |  1  | -1  |  3  |  4  |
  306.  
  307. This array contains the three sets S1, S2 and S6:
  308.  
  309.                     1           2           6
  310.                    / \          |
  311.                   3   5         4
  312.                  /              |
  313.                 7               8
  314.  
  315. "find" returns the number of the root element (= the identifier of the set)
  316. of the tree in which the given element is contained:
  317.  
  318.       find(a) := i  so that  a in Si
  319.  
  320. It also reduces the height of that tree by changing all the pointers from
  321. the given element up to the root element to point DIRECTLY to the root
  322. element.
  323.  
  324. Example:
  325.  
  326.     find(7) returns "1" and modifies the array as follows:
  327.  
  328.             i  : |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |
  329.     -------------+-----+-----+-----+-----+-----+-----+-----+-----+
  330.      parent[i] : | -4  | -3  |  1  |  2  |  1  | -1  |  1  |  4  |
  331.  
  332.                     1           2           6
  333.                    /|\          |
  334.                   3 5 7         4
  335.                                 |
  336.                                 8
  337.  
  338. "union" takes the identifiers of two sets (= the numbers of their respective
  339. root elements) and merges the two sets by appending one of the two trees to
  340. the other. It always appends the SMALLER set to the LARGER one (to keep the
  341. height of the resulting tree as small as possible) and updates the number of
  342. elements contained in the resulting set which is stored in the root element's
  343. cell of the array.
  344.  
  345. Example:
  346.  
  347.     union(2,6) does the following:
  348.  
  349.             i  : |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |
  350.     -------------+-----+-----+-----+-----+-----+-----+-----+-----+
  351.      parent[i] : | -4  | -4  |  1  |  2  |  1  |  2  |  1  |  4  |
  352.  
  353.                     1           2
  354.                    /|\         / \
  355.                   3 5 7       4   6
  356.                               |
  357.                               8
  358.  
  359.     complexity for O(n) "find" operations: O( G(n) n )
  360.  
  361.     complexity for one "union" operation: O(1)
  362.  
  363.     complexity for O(n) ( "find" + "union" ) operations: O( G(n) n )
  364.  
  365.     where  G(n) := min{ j | F(j) >= n }
  366.  
  367.     and    F(j) := 1            for j = 0
  368.            F(j) := 2 ^ F(j-1)   for j > 0
  369.  
  370.     also,  G(n) <= ld n         for all n
  371.  
  372. =item *
  373.  
  374. C<&kruskal();>
  375.  
  376. This routine carries out the computations associated with Kruskal's algorithm.
  377.  
  378. Returns an array of hashes (each hash containing the keys "from", "to" and
  379. "cost" and the corresponding values) representing the minimal spanning tree
  380. of the graph previously defined by calls to "define_vortices" and
  381. "define_edges".
  382.  
  383. The result can also be found in @Graph::Kruskal::T.
  384.  
  385. See the implementation of the subroutine "example" to see how to access this
  386. array directly (remember to fully qualify the name of this array in your
  387. program, i.e., use "@Graph::Kruskal::T" instead of just "@T", since this array
  388. is not exported - or your program will not work!)
  389.  
  390. =item *
  391.  
  392. C<&example();>
  393.  
  394. Demonstrates how to use the various subroutines in this module.
  395.  
  396. Computes the minimal spanning tree of a sample graph.
  397.  
  398. Just say "use Graph::Kruskal qw(example);" and "&example();" in a little
  399. Perl script to see it "in action".
  400.  
  401. =back
  402.  
  403. =head1 DESCRIPTION
  404.  
  405. This algorithm computes the Minimal Spanning Tree of a given graph
  406. according to some cost function defined on the edges of that graph.
  407.  
  408. Input: A set of vortices which constitute a graph (some cities on a map,
  409. for example), a set of edges (i.e., roads) between the vortices of the
  410. (non-directed and connected) graph (i.e., the edges can be traveled in
  411. either direction, and a path must exist between any two vortices), and
  412. the cost of each edge (for instance, the geographical distance).
  413.  
  414. Output: A set of edges forming a spanning tree (i.e., a set of edges linking
  415. all vortices, so that a path exists between any two vortices) which is free
  416. of circles (because it's a tree) and which is minimal in terms of the cost
  417. function defined on the set of edges.
  418.  
  419. See Aho, Hopcroft, Ullman, "The Design and Analysis of Computer Algorithms"
  420. for more details on the algorithm.
  421.  
  422. =head1 SEE ALSO
  423.  
  424. Math::MatrixBool(3), Math::MatrixReal(3), DFA::Kleene(3),
  425. Set::IntegerRange(3), Set::IntegerFast(3), Bit::Vector(3).
  426.  
  427. =head1 VERSION
  428.  
  429. This man page documents "Graph::Kruskal" version 2.0.
  430.  
  431. =head1 AUTHOR
  432.  
  433. Steffen Beyer <sb@sdm.de>.
  434.  
  435. =head1 COPYRIGHT
  436.  
  437. Copyright (c) 1995, 1996, 1997 by Steffen Beyer. All rights reserved.
  438.  
  439. =head1 LICENSE
  440.  
  441. This package is free software; you can redistribute it and/or modify
  442. it under the same terms as Perl itself.
  443.  
  444.